// pages/zhu_search/zhu_search.js
let util = require('../../utils/util');
const api = require('../../utils/api');
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: {},
    goods: {},
    goodsImg: {},
    beginIndex: 0,
    // endIndex: 3,
    endIndex: 0,
    size: 3,
    goodsLen: 0,
    recordList: [],//记录搜索关键词
    
  },
  //点击图片跳到详情页
  targepro(e){
    let id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: "../productInfo/productInfo?id=" + id
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    console.log(options);
    let that = this;
      util.myRequest(api.goodsList, {
         keyword:options.keyword,
        // isHot: 1,
        page: 1,
        size: 10,
      }, res => {
        wx.hideLoading();
        if (that.data.goodsImg == "" || res.data.data.data == ""){
          wx.showToast({
            title: "没有此类商品",
            icon: 'none',
            duration: 1500,
            mask: false,
            complete: function() {
              setTimeout(function(){
                wx.navigateBack({
                  delta: 1
                });
              },1500)
            }
          });
        }else{
          that.setData({
            goodsLen: res.data.data.data.length,
            goodsList: res.data.data.data,
            goods: res.data.data.data.slice(0, 2),
            goodsImg: res.data.data.data.slice(0, 3),
          })
          if(that.data.goodsLen < 3){
            that.setData({
              endIndex: that.data.goodsLen
            })
          }else{
            that.setData({
              endIndex: 3
            })
          }
          //保存搜索记录关键词
          let newList = [];
          let recordData = wx.getStorageSync("recordList");
          if(recordData.length > 50){

          }
          if (recordData){
            recordData.forEach(function (item) {
              if (options.keyword != item){
                newList.push(item);
              }
            })
            newList.push(options.keyword);
            that.setData({
              recordList: newList
            })
          }else{
            that.setData({
              ['recordList[0]']: options.keyword
            })
          }
          wx.setStorageSync("recordList", that.data.recordList);
        }
      })
  },
  /**
   * 换一换
   */
  onClick() {
    var that = this;
    if (that.data.endIndex < 3){
      wx.showToast({
        title: '已经没有更多了',
        icon: "none"
      })
    }else{
    let
      beginIndex = this.data.beginIndex + 3,
      endIndex = this.data.endIndex + 3;

    if (beginIndex > endIndex) {
      beginIndex = endIndex-3;
    }
    if (endIndex > that.data.goodsLen) {
      endIndex = endIndex - that.data.goodsLen; // 12 -10
    }
    this.setData({
      beginIndex,
      endIndex,
    })
    }
    // if(that.data.size < that.data.goodsLen-3){
    //   that.setData({
    //     size: that.data.size + 3,
    //   },function(){
    //     that.setData({
    //       goodsImg: that.data.goodsList.slice(that.data.size,that.data.size+3)

    //     })
    //   })
    //   console.log(that.data.goodsImg)

    // }else{
    //   wx.showToast({
    //     title: "没有更多了",
    //     icon: 'none',
    //     duration: 1500,
    //     mask: false,
    //   });
    // }

    
  },
  targepro(e){
    console.log(e);
    console.log(e.currentTarget.dataset.id)
    let that = this;
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/productInfo/productInfo?id="+id,
    });
  }
})